package com.jeesite.modules.wzt.util;

import com.banboocloud.Codec.BamboocloudFacade;
import com.jeesite.common.config.Global;
import com.jeesite.common.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public abstract class BamboocloudUtils {

    public static boolean checkUsernamePassword(String username, String password) {
        String bimRemoteUser = Global.getConfig("platform.wzt.bimRemoteUser");
        String bimRemotePwd = Global.getConfig("platform.wzt.bimRemotePwd");
        if(StringUtils.equals(username,bimRemoteUser)&&StringUtils.equals(password,bimRemotePwd)){
            return true;
        }else{
            return false;
        }
    }

    public static String getPlaintext(String ciphertext, String key, String type) {
        return BamboocloudFacade.decrypt(ciphertext, key, type);
    }

    public static Boolean verify(Map<String, Object> reqmap, String type) {
        Map<String, Object> verifymap = new TreeMap<String, Object>();
        StringBuffer sb = new StringBuffer();
        Iterator<String> it = reqmap.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            verifymap.put(key, reqmap.get(key));
        }
        Iterator<String> ittree = verifymap.keySet().iterator();
        while (ittree.hasNext()) {
            String key = (String) ittree.next();
            if (!"signature".equals(key)) {
                sb.append(key).append("=").append(verifymap.get(key)).append("&");
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        System.out.println(reqmap.get("signature") + "  now  " + sb.toString());
        return BamboocloudFacade.verify(reqmap.get("signature").toString(), sb.toString(), type);
    }

    public static String getRequestBody(HttpServletRequest request) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String str = "";
        try {
            br = request.getReader();
            while ((str = br.readLine()) != null) {
                sb.append(str);
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
            if (br != null)
                try {
                    br.close();
                } catch (IOException eo) {
                    eo.printStackTrace();
                }
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
    /**     * 实现将源类属性拷贝到目标类中     *      * @param Map map     * @param Object obj     */
    public static void copyProperties(Map map, Object obj) throws Exception {
        // 获取目标类的属性信息
        BeanInfo targetbean = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = targetbean.getPropertyDescriptors();
        // 对每个目标类的属性查找set方法，并进行处理
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor pro = propertyDescriptors[i];
            Method wm = pro.getWriteMethod();
            if (wm != null) {// 当目标类的属性具有set方法时，查找源类中是否有相同属性的get方法
                Iterator ite = map.keySet().iterator();
                while (ite.hasNext()) {
                    String key = (String) ite.next();
                    // 判断匹配
                    if (key.equals(pro.getName())) {
                        if (!Modifier.isPublic(wm.getDeclaringClass().getModifiers())) {
                            wm.setAccessible(true);
                        }
                        Object value = (String) map.get(key);
                        // 调用目标类对应属性的set方法对该属性进行填充
                        wm.invoke((Object) obj, new Object[]{value});
                        break;
                    }
                }
            }
        }
    }

}